package de.hdu.myjobapp.view.view;

import java.util.Date;
import java.util.Vector;

import com.vaadin.data.Validator;
import com.vaadin.data.util.BeanItem;
import com.vaadin.data.validator.RegexpValidator;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.themes.Reindeer;
import com.vaadin.ui.Button;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Layout;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

import de.hdu.myjobapp.ctrl.ApplicationController;
import de.hdu.myjobapp.model.Application;
import de.hdu.myjobapp.model.Rating;
import de.hdu.myjobapp.view.MyJobApplication;
/**
 * Simple Editor Form to edit basic Ratings for applications
 * @author MTA
 *
 */
public class RatingForm extends Window implements ClickListener{
	private Rating rating;
	private Application application;
	private BeanItem<Rating> ratingBean; 
	final Form form = new Form();
	private Button delete = new Button("Löschen");
	private Button save = new Button("Speichern");
	private Boolean neworold = false; //false ist old bzw. bestehend
	private ApplicationController appControl = (ApplicationController)MyJobApplication.getProject().getApplicationContext().getBean("applicationController");

	/**
	 * Overloaded Constructor to be able to edit existing Ratings
	 */
	public RatingForm(Application app, Rating rating){
		super("Ratingfenster");
		this.rating = rating;
		this.application = app;
		this.neworold = false;
		buildview();
		
	}
	/**
	 * Standard Constructor to build a view to create a new Rating
	 * relating to an application
	 */
	public RatingForm(Application app){
		super("Ratingfenster");
		this.rating = new Rating();
		this.application = app;
		this.neworold = true;
		buildview();
	}
	/**
	 * Create the Layout of the new Window
	 */
	private void buildview(){
		Layout main = new VerticalLayout();
		setContent(main);
		main.setSizeUndefined();
		main.setStyleName(Reindeer.PANEL_LIGHT);
		
		ratingBean = new BeanItem<Rating>(this.rating);
		
		 
		 // Set form caption and description texts 
		form.setCaption("Rating");
		form.setDescription("Bitte geben Sie eine Beschreibung und eine Bewertung ihres Ratings ein. Die Bewertung ist numerisch. Mehr Punkte zählen mehr.");

		 // Bind the bean item as the data source for the form. 
		form.setItemDataSource(ratingBean); 
		Vector reihenfolge = new Vector();
		reihenfolge.add("keyWord");
		reihenfolge.add("ratingGrad");
		form.setVisibleItemProperties(reihenfolge);
		
		//Make the two fields required
		form.getField("keyWord").setRequired(true);
		form.getField("keyWord").setRequiredError("Beschreibung muss ausgefüllt werden");
		form.getField("ratingGrad").setRequired(true);
		form.getField("ratingGrad").setRequiredError("Es muss eine Bewertung abgegeben werden");
		
		// Create the Validator
		Validator ratingGradValidator = new RegexpValidator(
		    "[0-9]{1}", "Bitte eine Bewertung von 0 - 10 eingeben.");
		form.getField("ratingGrad").addValidator(ratingGradValidator);
		
		// Enable buffering.
		form.setWriteThrough(false);
		
		main.addComponent(form);
		Layout buttons = new HorizontalLayout();
		delete.addListener(this);
		//neue Ratings können nicht gelöscht werden
		delete.setEnabled(!neworold);
		buttons.addComponent(delete);
		save.addListener(this);
		buttons.addComponent(save);
		main.addComponent(buttons);
	}
	
	
	@Override
	public void buttonClick(ClickEvent event) {
		if (event.getButton() == delete)
		{
			if(!neworold){
				appControl.deleteRating(rating);
				//Gibt eine Nachricht am Bildschirm aus
				MyJobApplication.getProject().getMainWindow().showNotification("Rating gelöscht");
				//aktualisiert die Tabelle
				MyJobApplication.getProject().getUiHandler().setApplicationEditorApp(application, false);
			}
		}
		else if (event.getButton() == save)
		{
			if(neworold){
				form.commit();
				rating.setApplicationId(application);
				rating.setOrgId(application.getOrgId());
				appControl.saveRating(rating);
				MyJobApplication.getProject().getUiHandler().setApplicationEditorApp(application, false);
			}else{
				form.commit();
				appControl.saveRating(rating);
				MyJobApplication.getProject().getUiHandler().setApplicationEditorApp(application, false);
			}
			
		}
		if (getParent() != null)
		{
			((Window) getParent()).removeWindow(this);
		}
		
	}

}
